package com.nullpointer.playerinfo.bukkit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.scheduler.BukkitRunnable;

import com.nullpointer.playerinfo.common.Methods;
import com.nullpointer.playerinfo.common.exception.PlayerInfoException;
import com.nullpointer.playerinfo.common.objects.Player;
import com.nullpointer.playerinfo.common.objects.PluginMode;

public class Commands implements TabExecutor {

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {

        if (args.length == 1) {
            List<String> result = new ArrayList<>();
            if (sender.hasPermission("playerinfo.admin")) {
                result.addAll(new ArrayList<>(Arrays.asList("getfirstplaytime", "getlastplaytime", "getfirstplaytimeformatted", "getlastplaytimeformatted",
                "getcash", "getexp", "getcash", "getlevel", "getonlinetime", "gettotalonlinetime", "getonlinetimefomatted", "gettotalonlinetimefomatted",
                "getupgradeexp", "addexp", "addcash", "removecash", "removedata")));
            }
            result.removeIf(item -> !item.startsWith(args[0].toLowerCase()));
            return result;
        } else if (args.length == 2) {
            List<String> result = new ArrayList<>();
            for (org.bukkit.entity.Player p : Bukkit.getOnlinePlayers()) {
                result.add(p.getName());
            }
            result.removeIf(item -> !item.startsWith(args[1].toLowerCase()));
            return result;
        }
        return Collections.emptyList();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 2) {
            switch (args[0].toLowerCase()) {
                case "getfirstplaytime":
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getFirstPlayTime()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "getlastplaytime":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getLastPlayTime()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;
                
                case "getfirstplaytimeformatted":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(player.getFirstPlayTimeFormatted());
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "getlastplaytimeformatted":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(player.getLastPlayTimeFormatted());
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "getcash":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getCash()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "getexp":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getExp()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "getlevel":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getLevel()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "getonlinetime":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getOnlineTime()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "gettotalonlinetime":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getTotalOnlineTime()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;
                
                case "getonlinetimefomatted":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getOnlineTimeFomatted()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "gettotalonlinetimefomatted":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getTotalOnlineTimeFomatted()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "getupgradeexp":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                sender.sendMessage(String.valueOf(player.getUpgradeExp()));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;

                case "removedata":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                player.removeData();
                                sender.sendMessage("已将玩家" + player.getName() + "的数据抹除！！");
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            }
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
                    break;
                
                default:
                    sender.sendMessage("语法错误！！！");
                    break;
            }

        } else if (args.length == 3) {
            switch (args[0].toLowerCase()) {
                case "addexp":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                int exp = Integer.parseInt(args[2]);
                                player.addExp(exp);
                                sender.sendMessage("为玩家" + player.getName() + "增加" + exp + "点经验值！！！");
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            } catch (NumberFormatException e) {
                                sender.sendMessage("参数错误！！！");
                                return;
                            }
                            
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);

                    break;

                case "removeexp":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                int exp = Integer.parseInt(args[2]);
                                player.removeExp(exp);
                                sender.sendMessage("扣除玩家" + player.getName() + exp + "点经验值！！！");
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            } catch (NumberFormatException e) {
                                sender.sendMessage("参数错误！！！");
                                return;
                            }
                            
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);

                    break;
                
                case "addcash":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                int cash = Integer.parseInt(args[2]);
                                sender.sendMessage("玩家" + player.getName() + "当前金币：" + player.getCash());
                                sender.sendMessage("增加中...");
                                player.addCash(cash);
                                sender.sendMessage("增加成功！玩家" + player.getName() + "当前金币：" + player.getCash());
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            } catch (NumberFormatException e) {
                                sender.sendMessage("参数错误！！！");
                                e.printStackTrace();
                                return;
                            }
                            
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);

                    break;

                case "removecash":
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUKKIT);
                                int cash = Integer.parseInt(args[2]);
                                sender.sendMessage("玩家" + player.getName() + "当前金币：" + player.getCash());
                                sender.sendMessage("扣除中...");
                                player.removeCash(cash);
                                sender.sendMessage("扣除成功！玩家" + player.getName() + "当前金币：" + player.getCash());
                            } catch (PlayerInfoException e) {
                                sender.sendMessage("没有找到该玩家！！！");
                            } catch (NumberFormatException e) {
                                sender.sendMessage("参数错误！！！");
                                e.printStackTrace();
                                return;
                            }
                            
                        }
                        
                    }.runTaskAsynchronously(PlayerInfoBukkit.plugin);

                    break;
                default:
                    sender.sendMessage("语法错误！！！");
                    break;
            }
        }


        return true;
    }
    
}
